﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Diagnostics;

namespace LowLevelGraphics.ColorSpaces
{
    [DebuggerDisplay("H: {m_H}, G: {m_G}, Ö: {m_L}")]
    public class HGL : AbstractColorSpace
    {
        protected double m_H = 0.0f;
        protected double m_G = 0.0f;
        protected double m_L = 0.0f;

        /// <summary>
        /// primary constructor
        /// </summary>
        /// <param name="_color"></param>
        public HGL(Color _color)
            : base(_color)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="IRgBy"/> class.
        /// </summary>
        /// <param name="_I">The _ I.</param>
        /// <param name="_Rg">The _ rg.</param>
        /// <param name="_By">The _ by.</param>
        /// <param name="_hue">The _hue.</param>
        public HGL(double _H, double _G, double _L)
        {
            this.m_H = _H;
            this.m_G = _G;
            this.m_L = _L;
        }

        /// <summary>
        /// Used by clone to create a deep copy
        /// </summary>
        /// <param name="_irgby"></param>
        internal HGL(HGL _irgby)
        {
            this.m_Color = _irgby.m_Color;
            this.m_H = _irgby.m_H;
            this.m_G = _irgby.m_G;
            this.m_L = _irgby.m_L;
        }

        /// <summary>
        /// enables you to cast rgb color into this color space
        /// </summary>
        /// <param name="_color"></param>
        /// <returns></returns>
        public static explicit operator HGL(Color _color)
        {
            return new HGL(_color);
        }

        /// <summary>
        /// Gets or sets I
        /// </summary>
        public double H
        {
            get { return m_H; }
            set { m_H = value; }
        }

        /// <summary>
        /// Gets or sets Rg
        /// </summary>
        public double G
        {
            get { return m_G; }
            set { m_G = value; }
        }

        /// <summary>
        /// Gets or sets By
        /// </summary>
        public double L
        {
            get { return m_L; }
            set { m_L = value; }
        }

    
        /// <summary>
        /// Normalize
        /// </summary>
        /// <returns></returns>
        public override AbstractColorSpace Normalize()
        {
            return base.Normalize();
        }

        /// <summary>
        /// Convert to this color space
        /// </summary>
        protected override void CalcValues()
        {
            //M = Math.Max(Math.Max(m_Color.R, m_Color.G), m_Color.B);
            //m = Math.Min(Math.Min(m_Color.R, m_Color.G), m_Color.B);
            //C = M - m;

            m_H = (Math.Log(m_Color.R) + Math.Log(m_Color.B) + Math.Log(m_Color.G)) / 3;
            m_G = Math.Log(m_Color.R) - Math.Log(m_Color.G);
            m_L = Math.Log(m_Color.B) - (Math.Log(m_Color.G) + Math.Log(m_Color.R)) / 2;
        }

        /// <summary>
        /// Convert back to RGB color space
        /// </summary>
        /// <returns></returns>
        public override Color ToColor()
        {
            int H = 0;
            int G = 0;
            int L = 0;
            return Color.FromArgb(0, 0, 0);
        }

        /// <summary>
        /// Gets the values.
        /// </summary>
        /// <returns></returns>
        public override object[] GetValues()
        {
            return new object[] { m_H, m_G, m_L };
        }

        /// <summary>
        /// Sets the values.
        /// </summary>
        /// <param name="_aObject">The _a object.</param>
        public override void SetValues(object[] _aObject)
        {
            this.m_H = (double)_aObject[0];
            this.m_G = (double)_aObject[1];
            this.m_L = (double)_aObject[2];
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return string.Format("Color: H: {0}, G: {1}, L: {2}", m_H, m_G, m_L);
        }

        /// <summary>
        /// Used to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new HGL(this);
        }
    }
}
